package org.groovymud.object.registry.domain;

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;

import java.util.List;

import org.groovymud.object.registry.MudObjectAttendant;
import org.groovymud.object.registry.Registry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

public abstract class AbstractSuffixedScriptedDomainManager<T> implements DomainManager<T> {
	@Autowired
	private MudObjectAttendant attendant;
	@Autowired
	private Registry registry;
	@Autowired
	private GroovyScriptEngine engine;
	private ApplicationContext applicationContext;
	private String mudSpace;
	private String scriptSpace;
	private String store;
	
	private List<String> domainSuffixe;

	public boolean matchesSuffixe(String domain) {
		for (String suffix : domainSuffixe) {
			if (domain.endsWith(suffix)) {
				return true;
			}
		}
		return false;
	}

	public void setDomainSuffixe(List<String> suffixe) {
		this.domainSuffixe = suffixe;
	}

	public List<String> getDomainSuffix() {
		return domainSuffixe;
	}
	
	protected Binding createScriptBinding() {
		Binding binding = new Binding();
		binding.setVariable("mudSpace", getMudSpace());
		binding.setVariable("store", getStore());
		binding.setVariable("scriptSpace", getScriptSpace());
		binding.setVariable("context", getApplicationContext());
		binding.setVariable("registry", getRegistry());
		binding.setVariable("attendant", getAttendant());
		binding.setVariable("gse", getEngine());
		return binding;
	}

	
	/**
	 * @return the attendant
	 */
	public MudObjectAttendant getAttendant() {
		return attendant;
	}

	
	/**
	 * @param attendant the attendant to set
	 */
	public void setAttendant(MudObjectAttendant attendant) {
		this.attendant = attendant;
	}

	
	/**
	 * @return the registry
	 */
	public Registry getRegistry() {
		return registry;
	}

	
	/**
	 * @param registry the registry to set
	 */
	public void setRegistry(Registry registry) {
		this.registry = registry;
	}

	
	/**
	 * @return the engine
	 */
	public GroovyScriptEngine getEngine() {
		return engine;
	}

	
	/**
	 * @param engine the engine to set
	 */
	public void setEngine(GroovyScriptEngine engine) {
		this.engine = engine;
	}

	
	/**
	 * @return the applicationContext
	 */
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	
	/**
	 * @param applicationContext the applicationContext to set
	 */
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	
	/**
	 * @return the mudSpace
	 */
	public String getMudSpace() {
		return mudSpace;
	}

	
	/**
	 * @param mudSpace the mudSpace to set
	 */
	public void setMudSpace(String mudSpace) {
		this.mudSpace = mudSpace;
	}

	
	/**
	 * @return the scriptSpace
	 */
	public String getScriptSpace() {
		return scriptSpace;
	}

	
	/**
	 * @param scriptSpace the scriptSpace to set
	 */
	public void setScriptSpace(String scriptSpace) {
		this.scriptSpace = scriptSpace;
	}

	
	/**
	 * @return the store
	 */
	public String getStore() {
		return store;
	}

	
	/**
	 * @param store the store to set
	 */
	public void setStore(String store) {
		this.store = store;
	}

	
	/**
	 * @return the domainSuffixe
	 */
	public List<String> getDomainSuffixe() {
		return domainSuffixe;
	}

}
